﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Daco.Web.Handlers
{
    /// <summary>
    /// Summary description for PhotoHandler
    /// </summary>
    public class PHOTOHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;

            if (!string.IsNullOrEmpty(request["pid"]))
            {
                if (request["pid"].ToUpper() == "FFFFFFFF")
                {
                    // Lay anh mac dinh
                    System.IO.FileStream fstr = System.IO.File.Open(context.Server.MapPath(string.Format("~/App_Themes/{0}/Images/NoImage.jpg", Config.CSiteConfig.Current.Theme)), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
                    byte[] content = new byte[fstr.Length];
                    fstr.Read(content, 0, content.Length - 1);
                    fstr.Close();

                    context.Response.ContentType = "image/jpeg";
                    context.Response.OutputStream.Write(content, 0, content.Length);
                    context.Response.End();
                }
                else
                {
                    Daco.Web.Store.CPhoto photo = new Daco.Web.Store.CPhoto(request["pid"]);

                    bool preview = true;
                    if (!string.IsNullOrEmpty(request["preview"]) && bool.TryParse(request["preview"], out preview))
                    { }

                    byte[] content = Daco.Web.Store.CPhoto.GetPhotoContent(photo.PhotoID, !preview);
                    context.Response.ContentType = "image/jpeg";
                    context.Response.OutputStream.Write(content, 0, content.Length);
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("dont try");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}